package com.sgb.kjwl.view.ui.activity.enterprise;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;

import com.sgb.kjwl.databinding.ActivityContanctsBinding;
import com.sgb.kjwl.view.ui.adapter.enterprise.ContanctsAdpater;
import com.sgb.kjwl.viewmodel.enterprise.AddContanctsViewModel;
import com.sgb.kjwl.viewmodel.enterprise.ConTactsViewModel;
import com.sgb.link.constant.RequestCode;
import com.sgb.link.router.PathUser;
import com.swgk.core.base.model.entity.ContactsEntitiy;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.io.Serializable;

@Route(path = PathUser.ContanctsActivity)
public class ContanctsActivity extends AppActivity {
    private ActivityContanctsBinding mBinding;
    private ConTactsViewModel mViewModel;
    private AddContanctsViewModel addContanctsViewModel;
    private ContanctsAdpater mAdpater;
    private boolean isLodMore;


    @Override
    protected void initView() {
        super.initView();
        mViewModel = new ViewModelProvider(this).get(ConTactsViewModel.class);
        addContanctsViewModel = new ViewModelProvider(this).get(AddContanctsViewModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_contancts);
        mBinding.setVariable(BR.viewmodel, mViewModel);
        mBinding.setLifecycleOwner(this);
        ((TextView) mBinding.include.findViewById(R.id.tv_no_data)).setText("暂无联系人信息哦");
        mViewModel.request = getIntent().getBooleanExtra("request", false);
        initRecyclerView();
        initObserver();
        initLister();
    }


    private void initRecyclerView() {
        mAdpater = new ContanctsAdpater(this);
        mBinding.recyclerViewTrand.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerViewTrand.setAdapter(mAdpater);
    }


    private void initObserver() {

        mViewModel.delMsg.observe(this, s -> {
            MToast.showToast(ContanctsActivity.this, "删除成功");
            isLodMore = false;
            mViewModel.getListdata(false);
        });

        mViewModel.ToastShow.observe(this, data -> {
            MToast.showToast(this, data);
        });

        mViewModel.showloading.observe(this, type_int -> {
            switch (type_int) {
                case 1:
                    DialogHelper.showProgressDialog(getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case 2:
                    DialogHelper.dismissProgressDialog();
                    break;
            }
        });
        addContanctsViewModel.showLoading.observe(this, type_int -> {
            switch (type_int) {
                case 1:
                    DialogHelper.showProgressDialog(getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case 2:
                    DialogHelper.dismissProgressDialog();
                    break;
            }
        });
        addContanctsViewModel.commitStatus.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                MToast.showToast(ContanctsActivity.this, s);
                isLodMore = false;
                mViewModel.getListdata(false);
            }
        });

        mViewModel.listdata.observe(this, listdata -> {
            mBinding.recyclerViewTrand.refreshComplete();
            if (isLodMore) {
                if (null != listdata && listdata.size() > 0) {
                    mBinding.recyclerViewTrand.loadMoreComplete();
                    mAdpater.addDatas(listdata);
                } else {
                    mBinding.recyclerViewTrand.setNoMore(true);
                }
                mBinding.recyclerViewTrand.setVisibility(View.VISIBLE);
            } else {
                mBinding.recyclerViewTrand.refreshComplete();
                if (null != listdata && listdata.size() > 0) {
                    mBinding.recyclerViewTrand.setVisibility(View.VISIBLE);
                    mBinding.include.setVisibility(View.GONE);
                    mAdpater.setDatas(listdata);
                    mBinding.recyclerViewTrand.setLoadingMoreEnabled(true);
                } else {
                    mAdpater.setDatas(listdata);
                    mBinding.recyclerViewTrand.setVisibility(View.VISIBLE);
                    mBinding.include.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void initLister() {
        mBinding.actionBar.setMenuFunctionClick(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                AddContanctsActivity.startactivity(getActivity());
            }
        });
        mAdpater.setOnItemClick(new AdapterOnItemClick<ContactsEntitiy>() {
            @Override
            public void onItemClick(ContactsEntitiy contactsEntitiy, int position) {
                switch (position) {
                    case -1://修改
                        AddContanctsActivity.startactivity(getActivity(), contactsEntitiy);
                        break;
                    case -2://删除
                        new RoundDialog.Builder(ContanctsActivity.this)
                                .title("提示")
                                .content("确定要删除该联系人信息吗？")
                                .cancel("取消")
                                .confirm("确定")
                                .contentGravity(Gravity.CENTER)
                                .cancelable(false)
                                .confirmCallback(v -> {
                                    mViewModel.delcontact(contactsEntitiy.getId() + "");
                                })
                                .show();
                        break;
                    case -3://设置默认
                        //发布类型 0个人  1企业  2团队
                        Integer publishType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
                        if (0 == publishType) {
                            contactsEntitiy.setUserNo(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
                        } else if (1 == publishType || 2 == publishType) {
                            contactsEntitiy.setCompanyNo(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, ""));
                        }
                        contactsEntitiy.setPublishType(publishType);
                        if ("0".equals(contactsEntitiy.getStatus())) {
                            contactsEntitiy.setStatus("1");
                        } else {
                            contactsEntitiy.setStatus("0");
                        }
                        addContanctsViewModel.data.setValue(contactsEntitiy);
                        addContanctsViewModel.submitData(1);
                        break;
                    default:
                        if (mViewModel.request) {
                            Intent mintent = new Intent();
                            mintent.putExtra("data", (Serializable) contactsEntitiy);
                            setResult(RequestCode.CODE_ZERO, mintent);
                            finish();
                        }
                        break;
                }
            }
        });
        mBinding.recyclerViewTrand.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isLodMore = false;
                mViewModel.getListdata(false);
            }

            @Override
            public void onLoadMore() {
                isLodMore = true;
                mViewModel.getListdata(true);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        isLodMore = false;
        mViewModel.getListdata(false);
    }

    public static void startactivity(Context mContext) {
        Intent mIntent = new Intent(mContext, ContanctsActivity.class);
        mContext.startActivity(mIntent);
    }


    public static void startactivityForResult(Activity mContext, int RequestCode) {
        Intent mIntent = new Intent(mContext, ContanctsActivity.class);
        mIntent.putExtra("request", true);
        mContext.startActivityForResult(mIntent, RequestCode);
    }
}
